<?php

namespace app\modules\admin\models;

use app\models\BaseUserModel;
use app\models\Userziliao;
use app\components\Fun;

class UserList extends \app\components\BaseViewList
{
    public function get_model()
    {
        return new  \app\models\User;
    }

    /**
     * 获取表格的唯一名字
     */
    public function getName()
    {
        return 'userlist';
    }


    /**
     * 自动检查是否有新数据,如果为0,则表示不自动检查,单位是秒
     */
    // public $auto_detection=10;

    /**
     * 数据中最大唯一值,用来动态检查数据更新
     */
    public function max_val()
    {
        $model = $this->find()->limit(1)->one();
        if ($model == null) {
            return '';
        }
        return $model->time;
    }


    /**
     * 方便重载,获取大于$maxid新的数据
     */
    public function get_new_data($maxId)
    {


        $model = $this->find()->andWhere('time>:id', [':id' => $maxId])->limit($this->PageSize)->all();
        $html = [];
        foreach ($model as $vo) {
            $html[] = ['html' => $this->echo_tbody_tr($vo),
                'list_id' => $this->model_to_id($vo)
            ];
        }
        // var_dump($html);exit;
        return $html;
    }

    /**
     * 返回查询的条件
     */
    public function find()
    {
        $find = $this->get_model()->find();

        if (isset($_GET['username']) && $_GET['username'] !== '') {
            $username = $_GET['username'];
            $find->andWhere([
                'or',
                ['like', 'username', "%$username%", false],
                ['id' => $_GET['username']]
            ]);
        }

        if (isset($_GET['role_type']) && $_GET['role_type'] !== '') {
            $find->andWhere(['role_type' => $_GET['role_type']]);
        }
        if (!empty($_GET['id'])) {
            $find->andWhere(['id' => $_GET['id']]);
        }


        $find->orderBy('time DESC');
        return $find;
    }


    public function dialog()
    {
        return array(
            array(
                'title' => '用户名',
                'zd' => 'username',
                ///'type'=>'textarea'
            ),


            array(
                'title' => '角色类型',
                'zd' => 'role_type',
                'type' => 'list',
                'data' => array(
                    \app\models\User::role_type_common => '普通',
                    \app\models\User::role_type_admin => '管理员'

                )
            ),
        );
    }


    /**
     * 需要继承,然后定义每行需要显示的数据格式
     */
    public function Labels()
    {
        return array(
            'id' => ['style' => 'width: 56px'],
            'username' => '',
            'time' => '',
            'introducer' => '',
            'type' => '',
            'role_type' => '',
            'control' => [
                'edit',
                'delete'
            ],

        );

    }


    /**
     * 方便继承,多一层过滤
     */
    public function ModelTohtml($key, $model)
    {

        if ($key === 'role_type') {
            return $this->toType(
                $this->get_model()->role_type_array()
                , $model->$key);

        }
        if ($key === 'bindtype') {


            return $this->toType(
                $this->get_model()->bindtype_array()
                , $model->$key);

        }
        if ($key == 'username') {
            return \app\models\User::getnameAvatar($model->id);
        }
        if ($key == 'time') {
            return $this->toTime($model->$key);
        }
        if ($key == 'regtime') {
            return $this->toTime($model->$key);
        }

        if ($key == 'type') {
            return $this->totypeli($model);

        }

        return $model->$key;
    }


    private function totypeli($model){
        $val=$this->toType([
            BaseUserModel::user_type_gongsi=>'公司',
            BaseUserModel::user_type_gongchang=>'工厂',
            BaseUserModel::user_type_geren=>'个人',
        ],$model->type);
        $html=$val.'(
<a class="ziliao_a" title="查看公司资料" target="_blank" href="/admin/view/userdatags?id='.$model->id.'">公司</a>
<a class="ziliao_a" title="查看工厂资料" target="_blank" href="/admin/view/userdatagc?id='.$model->id.'">工厂</a>
<a class="ziliao_a" title="查看个人资料" target="_blank" href="/admin/view/userdatagr?id='.$model->id.'">个人</a>
)';

        return $html;
    }





    /**
     *  如果删除列表中有管理员,则禁止删除
     */
    public function delete_model(&$ids)
    {

        $find = \app\models\User::find()->where(['in', 'id', $ids]);
        $find->andWhere(['role_type' => \app\models\User::role_type_admin]);
        $count = $find->count();

        if ($count !== '0') {
            \app\models\Error::$msg = '删除失败,不能删除管理员身份的用户';
            return false;
        }
        return true;
    }

    /**
     * 判断是否启用
     */
    public $BarChart = true;
    /**
     * 条形图使用的时间戳字段
     */
    public $fieldTime = 'time';
    /**
     * 其它条件限制
     */
    public $BarChartCondition = [
        '注册量' => '',    ///例子 前面是显示的文字

    ];

}
